﻿using System;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;
using System.Collections.Generic;

namespace Sinbiota.Web.Controllers
{
    [AdminPermission]
    public class HabitatController : BaseController
    {
        private readonly IHabitatPersistenceServices _habitatPersistenceServices;
        private readonly IUserPersistenceServices _userPersistenceServices;

        public HabitatController(IHabitatPersistenceServices habitatPersistenceServices, IUserPersistenceServices userPersistenceServices)
        {
            _habitatPersistenceServices = habitatPersistenceServices;
            _userPersistenceServices = userPersistenceServices;
        }

        //
        // GET: /Habitat/
        [ImportMessagesFromTempData]
        public ActionResult Index()
        {
            return View(_habitatPersistenceServices.FindAll());
        }


        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(_habitatPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }

        //
        // GET: /Habitat/Details/5

        [ImportMessagesFromTempData]
        public ActionResult Details(int id)
        {
            var habitat = _habitatPersistenceServices.FindById(id);
            return habitat == null ? View("NotFound") : View(habitat);
        }

        //
        // GET: /Habitat/Create

        [ImportMessagesFromTempData]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Habitat/Create

        [HttpPost, ExportMessagesToTempData]
        public ActionResult Create(Habitat habitat)
        {
            var newHabitat = new Habitat();
            TryUpdateModel(newHabitat);
            if (ModelState.IsValid)
            {
                _habitatPersistenceServices.Save(newHabitat);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(habitat);
        }

        //
        // GET: /Habitat/Edit/5

        [ImportMessagesFromTempData]
        public ActionResult Edit(int id)
        {
            var habitat = _habitatPersistenceServices.FindById(id);
            return habitat == null ? View("NotFound") : View(habitat);
        }

        //
        // POST: /Habitat/Edit/5

        [HttpPost, ExportMessagesToTempData]
        public ActionResult Edit(int id, Habitat habitat)
        {
            var editHabitat = _habitatPersistenceServices.FindById(id);
            TryUpdateModel(editHabitat);
            if (ModelState.IsValid)
            {
                _habitatPersistenceServices.Update(editHabitat);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(habitat);
        }

        //
        // GET: /Habitat/Delete/5

        [ImportMessagesFromTempData]
        public ActionResult Delete(int id)
        {
            var habitat = _habitatPersistenceServices.FindById(id);
            return habitat == null ? View("NotFound") : View(habitat);
        }

        //
        // POST: /Habitat/Delete/5

        [HttpPost, ExportMessagesToTempData]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var deleteHabitat = _habitatPersistenceServices.FindById(id);
            if (deleteHabitat == null) return View("NotFound");
            _habitatPersistenceServices.Delete(deleteHabitat);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }
    }
}
